import Decimal from "decimal.js";

export function roundNumber(
  num: number | string,
  decimalPlaces: number,
  needTrim = true
): string {
  const factor = new Decimal(num);

  let fixedString = factor
    .toFixed(decimalPlaces, Decimal.ROUND_HALF_CEIL)
    .toString();
  if (!needTrim) {
    return fixedString;
  } else {
    while (
      fixedString.includes(".") &&
      (fixedString.endsWith("0") || fixedString.endsWith("."))
    ) {
      fixedString = fixedString.slice(0, -1);
    }

    return fixedString;
  }
}
